# -*- encoding=utf8 -*-
        
__author__ = "Bryan.Chen"
__desc__=""
        
from airtest.core.api import *
        
auto_setup(__file__)
        
from poco.drivers.std import StdPoco
poco = StdPoco()
        
using("../FlowTest.air")
import FlowTest
using("SideMenu_Open.air")
import SideMenu_Open

import random
        
class SideMenu_Setting(FlowTest.BaseTestCase):
    def __init__(self):
        print("%s __init__" % (__file__))
        
    def runTest(self, args):
        print("%s runTest" % (__file__))
        # 打开左侧菜单
        isOpenSideMenu = SideMenu_Open.runTest({})
        if not isOpenSideMenu:
            return False

        # 获取左侧菜单节点
        bottomPanel = poco("bottom_panel")
        if not bottomPanel.exists():
            return False

        btnSetting = bottomPanel.offspring("settings_btn")
        if not btnSetting.exists():
            return False

        self.clickButton(btnSetting)

        try:
            poco("SystemSettingLayer").wait_for_appearance()
        except:
            assert(True, True, "系统设置面板正常弹出")
            return False


        settingLayer = poco("SystemSettingLayer")
        # 绑定手机
        btnBindPhone = settingLayer.offspring("Button_bind_phonenum")
        self.clickButton(btnBindPhone)

        bindPhoneLayer = settingLayer.offspring("PhoneBindLayer")
        if not bindPhoneLayer.exists():
            return False;
        btnClose = bindPhoneLayer.offspring("close_btn")
        self.clickButton(btnClose)

        # 音效设置
        btnAudioSetting = settingLayer.offspring("Button_game_setting")
        self.clickButton(btnAudioSetting)

        audioSettingLayer = settingLayer.offspring("SettingsLayer")
        if not audioSettingLayer.exists():
            return False;

        n = random.randint(0,1)
        if n == 0:
            btnSoundCheck = audioSettingLayer.offspring("sound_check")
            self.clickButton(btnSoundCheck)
        else:
            btnMusicCheck = audioSettingLayer.offspring("music_check")
            self.clickButton(btnMusicCheck)

        btnClose = audioSettingLayer.offspring("close_btn")
        self.clickButton(btnClose)

        # 游戏管理
        btnGameManage = settingLayer.offspring("Button_game_manager")
        self.clickButton(btnGameManage)

        gameManageLayer = settingLayer.offspring("GameManageLayer")
        if not gameManageLayer.exists():
            return False;

        gameList = gameManageLayer.offspring("game_list")
        gameItems = gameList.offspring("game_item_panel")
        gameCount = len(gameItems)
        if gameCount > 0:
            btnDel = gameItems[0].offspring("del_btn")
            self.clickButton(btnDel)

             # 等待确认弹框弹出
            try:
                poco("LBTipsLayer").wait_for_appearance()
            except:
                assert_equal(False, True, "修改性别确认框弹出正常")
                return False
            # 获取确认框
            confirmDialog = poco("LBTipsLayer")
            # 获取确认按钮
            btnConfirm = confirmDialog.offspring("ok_btn_2")
            if not btnConfirm.exists():
                return False
            # 点击确认按钮
            self.clickButton(btnConfirm)

            gameList = gameManageLayer.offspring("game_list")
            gameItems = gameList.offspring("game_item_panel")
            assert_equal(len(gameItems) == gameCount - 1, True, "删除子游戏成功")

        # 等待删除游戏
        sleep(1.0) 

        btnClose = gameManageLayer.offspring("back_btn")
        self.clickButton(btnClose)


        btnClose = settingLayer.offspring("close_btn")
        self.clickButton(btnClose)

        poco.click([0.7, 0.5])

        return True
        
_instance = SideMenu_Setting()
runTest = _instance.runTest